package com.codinglives.demoall.mulds.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Config;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.spring.annotation.MapperScan;

import javax.sql.DataSource;


/**
 * MyBatis的配置类
 *
 */
@Configuration
@AutoConfigureAfter({ DruidDataSource2Config.class })
@EnableTransactionManagement
@MapperScan(basePackages = "com.codinglives.demoall.mulds.mapper.sys",sqlSessionTemplateRef = "sqlSessionTemplate2")
public class Mybatis2Configuration  {

	private Logger logger = LoggerFactory.getLogger(Mybatis2Configuration.class);

    @javax.annotation.Resource(name = "dataSource2")
	private DataSource dataSource;

    @javax.annotation.Resource(name = "txManager2")
	private PlatformTransactionManager transactionManager;

	@Bean(name = "sqlSessionFactory2")
	public SqlSessionFactory sqlSessionFactory() {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		try {
			
			// 配置mapper的扫描，找到所有的mapper.xml映射文件
            Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath:/mapping/sys/*.xml");

			// mapper xml location
			bean.setMapperLocations(resources);

			bean.setTypeAliasesPackage("com.codinglives.demoall.mulds.model.sys");
			// pagehelper plugin
	        // bean.setPlugins(new Interceptor[] { pageHelper });
			
			SqlSessionFactory session = bean.getObject();
			
			
			MapperHelper mapperHelper = new MapperHelper();
			//特殊配置

			Config config = new Config();
			//具体支持的参数看后面的文档
			config.setNotEmpty(true);
			//设置配置
			mapperHelper.setConfig(config);
			// 注册自己项目中使用的通用Mapper接口，这里没有默认值，必须手动注册
			mapperHelper.registerMapper(Mapper.class);
			//配置完成后，执行下面的操作
			mapperHelper.processConfiguration(session.getConfiguration());
			
			
			return session;
		} catch (Exception e) {
			logger.error("配置SqlSessionFactory失败",e);
		}
		return null;
	}


	@Bean(name = "sqlSessionTemplate2")
	public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory2") SqlSessionFactory sqlSessionFactory) {
		return new SqlSessionTemplate(sqlSessionFactory);
	}


	@Bean("txManager2")
	public PlatformTransactionManager transactionManager(@Qualifier("dataSource2") DataSource dataSource) {
	    return new DataSourceTransactionManager(dataSource);
    }

    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return transactionManager;
    }
}
